package com.hsq.ndmall.common.utils;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;


import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2021/8/11 14:29
 */
public class ListFromEntity2VoUtils {

    /*

    {
        "id": 1005000,
        "name": "居家",
        "keywords": "",
        "desc": "desc",
        "pid": 0,
        "iconUrl": "http://yanxuan.nosdn.127.net/a45c2c262a476fea0b9fc684fed91ef5.png",
        "picUrl": "http://yanxuan.nosdn.127.net/e8bf0cf08cf7eda21606ab191762e35c.png",
        "level": "L1",
        "sortOrder": 2,
        "addTime": "2018-01-31T16:00:00.000+0000",
        "updateTime": "2018-01-31T16:00:00.000+0000",
        "deleted": false
        },
        {
            "id": 1005001,
            "name": "餐厨",
            "keywords": "",
            "desc": "desc",
            "pid": 0,
            "iconUrl": "http://yanxuan.nosdn.127.net/ad8b00d084cb7d0958998edb5fee9c0a.png",
            "picUrl": "http://yanxuan.nosdn.127.net/3708dbcb35ad5abf9e001500f73db615.png",
            "level": "L1",
            "sortOrder": 3,
            "addTime": "2018-01-31T16:00:00.000+0000",
            "updateTime": "2018-01-31T16:00:00.000+0000",
            "deleted": false
            },
     */
    public static <T> List<T> entity2Vo(List<Map> source, Class<? extends T> targetClazz) {
        List<T> targetList = new ArrayList();
        // 拿到的是Vo的属性名（id、name...）封装成一个数组
        Field[] targetFields = targetClazz.getDeclaredFields();
        Field.setAccessible(targetFields, true);
        try {
            // 遍历list集合中的map对象
            for (Map el : source) {
                // 拿到vo的实例
                T target = targetClazz.newInstance();
                for (Field targetField : targetFields) {
                    // 根据vo的属性名拿到值
                    Object value = el.get(targetField.getName());
                    //double类型转为BigDecimal
                    if (targetField.getType().equals(BigDecimal.class)) {
                        if (value != null) {
                            targetField.set(target, new BigDecimal(String.valueOf(value)));
                            continue;
                        }
                    }
                    //double类型转为BigDecimal
                    if (targetField.getType().equals(BigDecimal.class)) {
                        if (value != null) {
                            targetField.set(target, new BigDecimal(String.valueOf(value)));
                            continue;
                        }
                    }
                    //intger类型转boolean
                    if (targetField.getName().equals("isNew")) {
                        targetField.set(target, ((Integer) value).equals(new Integer(1)) ? true : false);
                        continue;
                    }
                    //intger类型转boolean
                    if (targetField.getName().equals("isHot")) {
                        targetField.set(target, ((Integer) value).equals(new Integer(1)) ? true : false);
                        continue;
                    }
                    //intger类型转boolean
                    if (targetField.getName().equals("isOnSale")) {
                        targetField.set(target, ((Integer) value).equals(new Integer(1)) ? true : false);
                        continue;
                    }
                    //如果deleted属性类型integer转boolean 1是删除 0是未删除
                    if (targetField.getName().equals("deleted")) {
                        //将转换的值设置到目标对象上
                        targetField.set(target, (value).equals(new Integer(1)) ? true : false);
                        continue;
                    }
                    //集合数据的填充
                    if (targetField.getType().equals(List.class)) {
                        continue;
                    }
                    // 日期时间填充
                    if (targetField.getType().equals(Date.class)) {
                        if (value != null) {
                            Date parse = null;
                            try {
                                parse = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse((String) value);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            targetField.set(target, parse);
                            continue;
                        }
                    }
                    //TODO 8.4.json数字字符串传字符串数组
                    if (targetField.getName().equals("gallery")||targetField.getName().equals("specifications")) {
                        //TODO 8.2.1.将转换的值设置到目标对象上
                        // 将
                        JSONArray jsonArray = JSONUtil.parseArray(value);
                        targetField.set(target,(String[])jsonArray.toArray(new String[]{}));
                        continue;
                    }
                    targetField.set(target, value);
                }
                targetList.add(target);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return targetList;
    }

    //public static void main(String[] args) {
    //    Map m1= new LinkedHashMap();
    //    m1.put("id",1);
    //    m1.put("name","kate");
    //    Map m2= new LinkedHashMap();
    //    m2.put("id",2);
    //    m2.put("name","kate2");
    //    List<Map> source= Arrays.asList(m1,m2);
    //
    //    ListFromEntity2VoUtils.entity2Vo(source,CategoryHomeVo.class).forEach(System.out::println);
    //}
}
















